package com.chaoxuan.uebt.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.adapter.WalletHistoryAdapter;

import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.model.Wallet;
import com.chaoxuan.uebt.model.WalletHistory;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

public class WalletActivity extends BaseActivity implements OnRefreshListener,OnLoadMoreListener,View.OnClickListener {
    WalletHistoryAdapter adapter;
    int page;
    TextView tvWallet,tvAllIncome,tvDigIncome;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);

        initTitle();
        tvWallet=(TextView)findViewById(R.id.tv_wallet);
        tvAllIncome=(TextView)findViewById(R.id.tv_all_income);
        tvDigIncome=(TextView)findViewById(R.id.tv_dig_income);
        findViewById(R.id.btn_apply).setOnClickListener(this);
        findViewById(R.id.btn_dig).setOnClickListener(this);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(linearLayoutManager);
        SmartRefreshLayout refreshLayout = (SmartRefreshLayout)findViewById(R.id.refreshLayout);

        adapter= new WalletHistoryAdapter();
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.autoRefresh();
        init();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_apply:
                startActivity(new Intent(this,WalletUebtActivity.class));
                break;
            case R.id.btn_dig:
                Const.TAB=1;
                startActivity(new Intent(this,MainActivity.class));
                finish();
                break;
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page=adapter.getData().size();
        load(false,refreshLayout);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page=0;
        load(true,refreshLayout);
    }

    private void load(final boolean refresh,final RefreshLayout refreshLayout){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        subscribe(RetrofitUtils.getAPIService().changeLists(user.getToken(),page,Const.DEVICE_TYPE), new BaseSubscriber<List<WalletHistory>>() {
            @Override
            public void onSuccess(List<WalletHistory> data) {
                if(refresh){
                    adapter.replaceData(data);
                    refreshLayout.finishRefresh();
                }else{
                    adapter.addData(data);
                    refreshLayout.finishLoadMore();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
    }

    private void init(){
        User user= CacheUtils.getInstance(this).getUser();
        if(user==null)return;
        subscribe(RetrofitUtils.getAPIService().wallet(user.getToken(),Const.DEVICE_TYPE), new BaseSubscriber<Wallet>() {
            @Override
            public void onSuccess(Wallet data) {
                tvWallet.setText(data.getWallet());
                tvAllIncome.setText(data.getAll_income());
                tvDigIncome.setText(data.getDig_income());
            }
        });
    }
}
